package com.leyou.user.web;

import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.AddressDTO;
import com.leyou.user.entity.Address;
import com.leyou.user.entity.Area;
import com.leyou.user.entity.City;
import com.leyou.user.entity.Province;
import com.leyou.user.service.AddressService;
import com.leyou.user.service.AreaService;
import com.leyou.user.service.CityService;
import com.leyou.user.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AreaService areaService;
    /**
     * 根据
     * @param id 地址id
     * @return 地址信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<AddressDTO> queryAddressById(@PathVariable("id") Long id){
        AddressDTO address = new AddressDTO();
        address.setId(id);
        address.setUserId(UserContext.getUser().getId());
        address.setStreet("航头镇航头路18号传智播客 3号楼");
        address.setCity("上海");
        address.setDistrict("浦东新区");
        address.setAddressee("社会我拓哥");
        address.setPhone("15800000000");
        address.setProvince("上海");
        address.setPostcode("210000");
        address.setIsDefault(true);
        return ResponseEntity.ok(address);
    }

    @GetMapping("/province")
    public ResponseEntity<List<Province>> province(){
        List<Province> list = provinceService.list();
        return ResponseEntity.ok(list);
    }

    @GetMapping("/city/{id}")
    public ResponseEntity<List<City>> city(@PathVariable("id")Long pid){
        List<City> list=cityService.findByCid(pid);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/area/{id}")
    public ResponseEntity<List<Area>> area(@PathVariable("id")Long cid){
        List<Area> list=areaService.findByCid(cid);
        return ResponseEntity.ok(list);
    }
    @Autowired
    private AddressService addressService;
    @PostMapping("add")
    public ResponseEntity<Void> area( Address address){
        addressService.saveAddress(address);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<com.leyou.user.entity.AddressDTO>> list(){
        List<com.leyou.user.entity.AddressDTO> list=addressService.findByUid();
        return ResponseEntity.ok(list);
    }

    @GetMapping("/delete")
    public ResponseEntity<Void> delete(@RequestParam("id") Long id){
        addressService.delete(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @GetMapping("/findById")
    public ResponseEntity<com.leyou.user.entity.AddressDTO> findById(@RequestParam("id") Long id){
        com.leyou.user.entity.AddressDTO dto=addressService.findById(id);
        return ResponseEntity.ok(dto);
    }

    @PostMapping("update")
    public ResponseEntity<Void> update( Address address){
        addressService.updateAddress(address);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @GetMapping("default")
    public ResponseEntity<Void> defaultAddress(@RequestParam Long id){

        addressService.defaultAddress(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}